/**
 * GoatListPlugin.java
 * 
 * FreeZzaph is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * FreeZzaph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.
 */

package freezzaph.plugins;

import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import freezzaph.exceptions.PluginException;
import freezzaph.exceptions.URLFetchException;
import freezzaph.plugins.util.HTMLEntityDecoder;
import freezzaph.plugins.util.URLFetcher;

/**
 * @author FreeZzaph
 */
public final class GoatListPlugin implements FreeZzaphDownloadPlugin {
	
	private static final Pattern category = Pattern.compile("^((?:\\w|\\s)+)\\s+-\\s+<a\\s+href=\"(movies/[^\"]+)1\\.shtml\">movie galleries</a>[^<]+?<a href=\"(pics/[^\"]+)1\\.shtml\">picture galleries</a>\\s*-", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
	
	private CategoryList pictures;
	private CategoryList videos;
	private HashMap<String, CategoryList> list;

	public HashMap<String, CategoryList> getCategoryLists() {
		if (pictures == null || videos == null || list == null) {
			pictures = new CategoryList("pictures", "Picture galleries");
			videos = new CategoryList("videos", "Video galleries");
			
			URL categoryURL = null;
			try {
				categoryURL = new URL("http://www.goatlist.com/");
			} catch (MalformedURLException e) {
				// Won't happen
			}
			
			String data;
			try {
				data = URLFetcher.fetch(categoryURL);
			} catch (URLFetchException e) {
				throw new PluginException("Could not download URL list. GoatList.com may not be accessible, or the plugin may need to be updated.", e);
			}
			
			Matcher m = category.matcher(data);
			while (m.find()) {
				
				// Extract data from match
				String decodedName = HTMLEntityDecoder.decode(m.group(1));
				String name = Category.prettify(decodedName);
				String description = decodedName;
				String moviePage = m.group(2);
				String picturePage = m.group(3);
				
				// Create picture and movie categories
				try {
					GoatListMovieCategory glmc = new GoatListMovieCategory(name, description, new URL(categoryURL, moviePage).toString());
					GoatListPictureCategory glpc = new GoatListPictureCategory(name, description, new URL(categoryURL, picturePage).toString());
										
					pictures.addCategory(name, glpc);
					videos.addCategory(name, glmc);
				} catch (MalformedURLException murle) {
					// Invalid URL, ignore.
				}
			}
			
			// Create list over category lists
			list = new HashMap<String, CategoryList>(2);
			list.put("pictures", pictures);
			list.put("videos", videos);
		}
		
		return list;
	}

	public String getPluginName() {
		return "goatlist";
	}

	public String getPluginTitle() {
		return "Goat List (the original Zzaph resource)";
	}

	/*
	 * (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#handleCommand(java.lang.String[])
	 */
	public boolean handleCommand(@SuppressWarnings("unused") String[] command, @SuppressWarnings("unused") PrintStream o) {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#showHelp()
	 */
	public String showHelp() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see freezzaph.plugins.FreeZzaphDownloadPlugin#provideSettings(freezzaph.plugins.FreeZzaphPluginPreferences)
	 */
	public void provideSettings(@SuppressWarnings("unused") FreeZzaphPluginPreferences settings) {
		// Ignore, we don't save any settings
	}

}
